// pages/user/user-order-page.js

const util = require('../../util/util.js');
const app = getApp()
import { RequestManager } from '../../util/requestManager.js'
let requestManager = new RequestManager()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    conentMarginTop: 0,
    selectedIndex: 1,   // 1表示订单已支付待配送 2表示已完成
    orderItems: [],
    move_animation: {},
    isSourceFromOrder:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    // console.log("options",options.source)

    app.globalData.needReload = true;
    var animation = util.moveXAnimation(0);
    this.setData({
      move_animation: animation.export(),
      conentMarginTop: app.globalData.navHeight,
      isSourceFromOrder: options.source === "order" ? true : false
    })
  },

  onShow: function () {
    if(app.globalData.needReload){
      this.private_downloadData()
    }
  },


  //  获取数据
  private_downloadData: function () {
    var that = this;
    requestManager.getUserOrderList(app.globalData.openid, reslut => {
      // console.log("订单列表数据...",reslut)
      that.setData({
        orderItems: reslut
      })
    });
  },

  //  自定义点击事件-切换订单状态
  action_switch: function (event) {

    let event_id = event.target.id;
    var screenW = app.globalData.screenW;

    var offset = event_id == "item-left" ? 0 : screenW/2;
    var animation = util.moveXAnimation(offset);

    this.setData({
      move_animation: animation.export(),
      selectedIndex: event_id == "item-left" ? 1 : 2
    })
    // console.log("event_id:", event_id)
  },
  //  自定义点击事件-查看订单详情
  action_detail: function (event) {
    var orderDetail = event.detail
    wx.navigateTo({
      url: '/pages/detail/user-order-detail-page?orderDetail=' + JSON.stringify(orderDetail),
    })
  },
  action_back:function(){
    wx.navigateBack({
      delta: 2
    })
  }
})